package cn.peach.pay.controller;

import cn.peach.basic.exception.MyException;
import cn.peach.basic.util.AjaxResult;
import cn.peach.pay.dto.AlipayNotifyDto;
import cn.peach.pay.service.IPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private IPayService payService;

    @PostMapping("/applyPay/{orderNo}")
    public AjaxResult applyPay(@PathVariable("orderNo") String orderNo){
        try {
            // 请求支付申请 返回的是一个HTML代码
            String htmlResult = payService.applyPay(orderNo);
            return AjaxResult.me().subResultObj(htmlResult);
        } catch (MyException e) {
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("支付申请不通过");
        }
    }

    /**
     * 异步通知
     * 支付宝调用 返回给支付宝
     * @param dto
     * @return
     */
    @RequestMapping("/notify")
    public String alipayNotify(AlipayNotifyDto dto){
        return payService.alipayNotify(dto);
    }
}
